home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14462 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  150 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c++,ualberta.cmput.201
  4. Subject: Re: class/function pointer help!
  5. Date: 31 Mar 1996 18:56:26 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4jmkgq$hei@dub-news-svc-3.compuserve.com>
  8. NNTP-Posting-Host: hd80-162.compuserve.com
  9.  
  10. ryangall@gpu.srv.ualberta.ca (Ryan Gallagher) s'Θcrit :
  11. > Hi, I have a class that uses a function pointer. The problem Im having is 
  12. > that the pointer doesnt want to access the function when the function is a 
  13. > local function to the calling class. Here is what I mean....
  14. > class expression
  15. >        {
  16. >         public:
  17. >                 string S;
  18. >         list L;
  19. >         .
  20. >         .
  21. >         int   initlist();
  22. >         .
  23. >         int   isexpr(int);
  24. >        };
  25. > list, and string are both classes.
  26. > char * string::POP( int (* separator)(int c) )
  27. > {
  28. >  int n=0,m=0,r,size=strlen(S);
  29. >  char *A,*B,*C;
  30. >   A=new char[size+1]; // allocate space for strings A & B
  31. >    .
  32. >    .
  33. >    
  34. >    
  35. >     //  find the first occurence of a separator character
  36. >     while(!(*separator)(A[n]) && A[n]) n++;
  37. >     .
  38. >     .
  39. >     .
  40. >   return A;
  41. > }
  42. > // heres the calling function
  43. > int expression::initlist(void)
  44. > {
  45. >  char *temp;
  46. >  float F;
  47. >  char C;
  48. >   L.init();
  49. >   while((temp=S.POP( ::isexpr )))
  50. >   {
  51. >     F=C=0;
  52. >     assert(convert(F,C,temp));
  53. >     L.Add(L.Assign(F,C));     
  54. >     delete[] temp;
  55. >   }
  56. >  return 0;
  57. > }
  58. > this works when I define isexpr(int) globally, but I want to beable to 
  59. > call function expression::isexpr(int).....when I do this...
  60. >   while((temp=S.POP( isexpr )))
  61. >   {
  62. >  the compiler say:" member function must be called or its address taken."
  63. > how do I declare the member function as a function pointer?!
  64. > thanks
  65. > ------
  66. > Please mail me back if possible.
  67. This is not a problem of your compiler, but a C++ language
  68. feature.
  69. >   
  70. A member function is not a standard function.
  71. So your isexpr() member function is not compatible with your
  72. S.POP() formal argument.
  73. This is because member functions have an hidden supplementary
  74. argument which references the instance on which it applies.
  75. The only way to manage with it would be to declare your
  76. member function isexpr() as a static member function, so that
  77. the instance reference is not passed. (So it will not be
  78. available in the body of that function).
  79.  
  80. Declare S.POP() like this :
  81. class string
  82. { ...
  83.   static char * POP( int (* separator)(int c) );
  84. }
  85.  
  86.  
  87. The other way is to declare the type of the instance reference
  88. (implicitly passed to your member function) within the
  89. argument of S.POP(). Like this, the pointed function will have
  90. access to its backgound instance.
  91.  
  92. Declare S.POP() like this :
  93. class string
  94. { ...
  95.   char * POP( int (expression::* separator)(int c) );
  96. }
  97.  
  98. This explictly declares that the function pointed by
  99. the "separator" argument, is a member of the "expression"
  100. class, and so it has an implicit argument which is an
  101. expression reference...
  102.  
  103. In summary, function pointers are not compatible to
  104. non static member function pointers.
  105. In addition, a method pointer from a given class is not compatible
  106. with a method pointer from another class which is not
  107. derived from the first one.
  108.  
  109. However, you can pass a pf method pointer like:
  110.   int (X::*pf)(U, V);
  111. as if it was in fact declared as :
  112.   int (Y::*pf)(U, V);
  113. provided X is derived from Y.
  114.  
  115. Its like if you had declared the function pointer:
  116.   static int (*pf)(X& x, U u, V v);
  117. and tried to pass it as an argument to a function which accepts
  118.   static int (*pf)(Y& y, U u, V v);
  119. and then use the x argument explicitly to access the members
  120. of the actual y parameter.
  121.  
  122. The last declaration used is what you would use, if you have
  123. to manage your pointers to several class types.
  124.  
  125. I hope this will help you...
  126.  
  127.